<template lang="pug">
  .clearfix
    //- 仅查看详情时显示
    .clearfix(v-if="isOnlyView")
      .form-shebao-title.font-16.font-weight 社保信息
      include ./formShebaoFilter.pug
      Table(border :columns="shebaoThead" :data="tableData" :stripe="true" ref="demoTable")
        .clearfix.pr-24.pl-24.font-14(slot-scope="{ row, index }" slot="leftTitle")
          span(v-if="index === 0") 基数
          span(v-if="index === 1") 比例

      .h24

      .form-shebao-title.font-16.font-weight 社保记录
      Table(border :columns="shebaojiluThead" :data="recordTableData" :stripe="true" ref="demoTable")

      .clearfix.text-center.mt-20.mb-20
        Button.mr-10(type="info" @click="handlepageUp") 查看上一页
        Button.mr-10(type="info" @click="handleModify") 修改
        Button(type="info" @click="handlepageDown" v-if="[12, 13, 123].indexOf(serviceFeeType) !== -1") 查看下一页

    //- 新建或修改时显示
    .clearfix(v-if="!isOnlyView")
      .form-shebao-title.font-16.font-weight 社会保险

      include ./formShebaoFilter.pug
      include ./formShebao.pug

      //- 参保信息设置页面时的按钮
      .clearfix.text-center.mt-20.mb-20(v-if="isInsuranceInfoSet")
        Button.mr-10(type="info" @click="handleSubmit") 保存
        Button(type="info" @click="goEmployeeBookList") 返回员工花名册列表

      //- 新建或修改时的按钮
      .clearfix.text-center.mt-20.mb-20(v-else)
        Button.mr-10(type="info" @click="handlepageUp") 上一步
        Button.mr-10(type="info" @click="handleSubmit") 保存
        Button(type="info" @click="handlepageDown" v-if="[12, 13, 123].indexOf(serviceFeeType) !== -1" :disabled="!isNext || !employeeId") 下一步
</template>

<script>
import { EmployeesApi } from '@/api/Employees.api.js'
import { SysWelfarePercentEmployeeConfigApi } from '@/api/SysWelfarePercentEmployeeConfig.api.js'
import { SysWelfarePercentAreaConfigApi } from '@/api/SysWelfarePercentAreaConfig.api.js'
import { FormShebaoFilter, FormShebaoFilterItem, FormShebaoTitle, FormShebao, FormShebaoItem } from '../EBformData.js'
import { shebaoThead, shebaojiluThead } from '../EBtableHead.js'
import _cloneDeep from 'clone-deep'
import { Common } from '@/utils/common.js'
import { EBbase } from '../EBbase.js'
export default {
  name: '',

  props: {
    serviceFeeType: {
      type: Number,
      default: 0
    },
    // companyId: {
    //   type: String,
    //   default: ''
    // },
    // companyName: {
    //   type: String,
    //   default: ''
    // },
    // employeeId: {
    //   type: String,
    //   default: ''
    // },
    isOnlyView: {
      type: Boolean,
      default: false
    },
    isInsuranceInfoSet: {
      type: Boolean,
      default: false
    }
  },

  data () {
    return {
      employeeId: '', // 员工ID
      companyId: '', // 公司ID
      companyName: '', // 公司名称
      insuranceId: '', // 保险ID
      formShebaoFilter: _cloneDeep(FormShebaoFilter),
      formShebaoFilterItem: _cloneDeep(FormShebaoFilterItem),
      formShebaoTitle: _cloneDeep(FormShebaoTitle),
      formShebao: _cloneDeep(FormShebao),
      formShebaoItem: _cloneDeep(FormShebaoItem),
      datePickerDefault: new Date(),
      rules: {},
      SecurityId: '', // 社保方案ID
      arrListDown: {
        securitys: [],
        securitysPlay1: []
      },
      shebaoThead: _cloneDeep(shebaoThead),
      shebaojiluThead: _cloneDeep(shebaojiluThead),
      tableData: [],
      recordTableData: [],
      selectedId: '',
      configInfo: {},
      isNext: false,
      dateOptions: {
        disabledDate (date) {
          const disabledDay = date.getDate()
          const month = date.getMonth()
          const nowMonth = (new Date()).getMonth()
          if (month === nowMonth) {

          } else {
            return disabledDay !== 1
          }
        }
      }
    }
  },

  watch: {
  },

  created () {
  },

  beforeMount () {

  },

  mounted () {
    this.employeeId = this.$route.query.employeeId // 员工ID
    this.companyId = this.$route.query.companyId // 公司ID
    this.companyName = this.$route.query.companyName // 公司名称

    EBbase.getAddModel(1, this.companyId, this.arrListDown)
    EBbase.getConfig(1, this.employeeId, this.configInfo, this.insuranceId, this.arrListDown, this.isOnlyView, this.formShebaoFilter, this.formShebao, this.tableData, this.formShebaoItem)
    console.log('EBbase.insuranceId: ', EBbase.insuranceId)
    console.log('💗 isModify: ', this.$route.query.isModify)
    // if (this.$route.query.isModify) { // isModify参数为true时，不限制下一步
    //   this.isNext = true
    // }
    this.isNext = !this.$route.query.isCreateNew || this.$route.query.employeeId
    console.log('员工社会保险this.isNext: ', this.isNext)
  },

  methods: {
    // 监听所有调用的方法
    listenCall (methodsWords, ...values) {
      this[methodsWords](...values)
    },

    async handleSubmit (name) {
      let formSum = { ...this.formShebaoFilter, ...this.formShebao }
      var tempAmounts = []

      for (var item in this.formShebao) {
        var aloneObg = {
          SecurityTypeCode: item,
          CompanyBaseAmount: this.formShebao[item].Company.baseAmount,
          EmployeeBaseAmount: this.formShebao[item].Employee.baseAmount
        }
        tempAmounts.push(aloneObg)
      }

      var params = {
        id: EBbase.insuranceId,
        securityType: 1, // 参保类型（1社会保险、2公积金）
        rangeGUID: this.formShebaoFilter.rangeGUID, // 社保编号
        employeeGUID: this.employeeId, // 员工ID
        securityGUID: this.formShebaoFilter.securityGUID, // 社保方案
        insuredTime: this.formShebaoFilter.insuredTime, // 参保时间
        description: this.formShebaoFilter.description, // 备注
        amounts: tempAmounts
      }
      let res = await SysWelfarePercentEmployeeConfigApi.saveShebaoOrGongjijin(params)
      console.log('提交表单res: ', res)
      if (res.status === 200 && res.data.success) {
        this.$Message.success(res.data.message)
        this.isNext = true
        this.$emit('saveSuccess', res.data.data)
      } else {
        this.$Message.error({
          content: res.data.message,
          duration: 10,
          closable: true
        })
      }
    },

    // 上一步
    handlepageUp () {
      Common.pageUp(this, 3) // '社保'的上一页，是'员工参保信息'，这个不会变
    },

    // 下一步
    handlepageDown () {
      if ([12, 23].indexOf(this.serviceFeeType) !== -1) { // 参保种类选择了'社保+社保' || '社保+社保+商保'
        Common.pageDown(this, 5) // 跳转到'社保'页面
      } else if (this.serviceFeeType === 13) { // 参保种类选择了'社保+商保'
        Common.pageDown(this, 6) // 跳转到'商保'页面
      } else {
        Common.pageDown(this)
      }
    },

    async getPlanList (selectId, securityType) {
      EBbase.getPlanList(selectId, securityType, this.arrListDown)
      this.formShebaoFilter.securityGUID = ''
    },

    // // 获取新增时参保区域基数配置信息列表
    async getAddModel2 (selectId, securityType, companyId, form, formItem) {
      EBbase.getAddModel2(selectId, securityType, this.companyId, this.formShebao, this.formShebaoItem)
    },

    // 表格选择操作
    handleOnSelectChange (value) {
      let arr = value.map((item) => {
        return item.id
      })
      this.selectedId = arr.join(',')
      console.log('列表勾选', this.selectedId)
    },

    // 修改
    handleModify () {
      this.isNext = true
      this.$emit('handleModify', 4)
    },

    // 输入框失去焦点时触发
    async inputOnBlur (form, sectionTitle, itemTitle, row, precision) {
      var params = {
        money: row.baseAmount,
        precision: precision,
        percent: row.percent
      }
      let res = await SysWelfarePercentAreaConfigApi.getTotalAmount(params)
      if (res.status === 200 && res.data.success) {
        this[form][sectionTitle][itemTitle].totalAmount = res.data.data
        console.log('--------------' + this[form][sectionTitle][itemTitle].totalAmount + '---------------')
      }
    },

    // 输入补缴月数计算参保时间
    toCountDate (form, month) {
      EBbase.toCountDate(this.formShebaoFilter, month)
    },

    // 输入参保时间计算补缴月数
    toCountMonth (form, date) {
      EBbase.toCountMonth(this.formShebaoFilter, date)
    },

    // 所有险种输入框失去焦点时触发
    allInsuranceOnBlue (form, sectionTitle, itemTitle, row, precision) {
      var tempArr = []
      Object.keys(this.formShebao).forEach(key => {
        if (this.formShebao[key].Company) {
          this.formShebao[key].Company.baseAmount = this.formShebao.AllInsurance.Company.baseAmount
          tempArr.push({
            money: this.formShebao[key].Company.baseAmount,
            precision: this.formShebao[key].Company.precision,
            percent: this.formShebao[key].Company.percent,
            securityTypeCode: key,
            isCompany: true
          })
        }
        if (this.formShebao[key].Employee) {
          this.formShebao[key].Employee.baseAmount = this.formShebao.AllInsurance.Company.baseAmount
          tempArr.push({
            money: this.formShebao[key].Employee.baseAmount,
            precision: this.formShebao[key].Employee.precision,
            percent: this.formShebao[key].Employee.percent,
            securityTypeCode: key,
            isCompany: false
          })
        }
      })
      SysWelfarePercentAreaConfigApi.getSecurityTotalAmountList({ Details: tempArr.slice(0, 16) }).then(res => {
        if (res.status === 200 && res.data.success) {
          console.log('data: ', res.data.data)
          res.data.data.map(item => {
            if (item.isCompany == true) {
              this.formShebao[item.securityTypeCode].Company.totalAmount = item.totalAmount
            } else {
              this.formShebao[item.securityTypeCode].Employee.totalAmount = item.totalAmount
            }
          })
        }
      })
    },

    // 返回员工花名册列表
    goEmployeeBookList () {
      EBbase.goEmployeeBookList(this.$router, this.companyId, this.companyName)
    }
  }
}
</script>

<style lang="less" scope>
</style>
